package com.dk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dk.mapper.AreaMapper;
import com.dk.pojo.Area;
import com.dk.pojo.AreaExample;
import com.dk.pojo.Location;
import com.dk.pojo.User;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.AreaService;
import com.dk.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public PageResult<Area> getAreaInfo(Integer pageNum, Integer pageSize, String code, String name, Integer positionX, Integer positionY, Integer positionZ, Integer lid) {
        if(!redisTemplate.hasKey("mesArea"+code+name+positionX+positionY+positionZ+lid)){
            AreaExample example = new AreaExample();
            if(lid != null && !"".equals(lid)){
                example.createCriteria().andLidEqualTo(lid);
            }
            if(positionX != null && !"".equals(positionX)){
                example.createCriteria().andPositionXEqualTo(positionX);
            }
            if(positionY != null && !"".equals(positionY)){
                example.createCriteria().andPositionYEqualTo(positionY);
            }
            if(positionZ != null && !"".equals(positionZ)){
                example.createCriteria().andPositionZEqualTo(positionZ);
            }
            if( name != null && name.length() > 0){
                example.createCriteria().andNameLike("%"+name+"%");
            }
            if( code != null && code.length() > 0){
                example.createCriteria().andCodeEqualTo(code);
            }
            List<Area> areas = areaMapper.selectByExample(example);
            redisTemplate.boundValueOps("mesArea"+code+name+positionX+positionY+positionZ+lid).set(JSONObject.toJSONString(areas));
            return getAreaPageResult(pageNum, pageSize, areas);
        }else {
            String o = (String) redisTemplate.boundValueOps("mesArea" + code + name + positionX + positionY + positionZ + lid).get();
            List<Area> areas = JSONObject.parseArray(o, Area.class);
            return getAreaPageResult(pageNum, pageSize, areas);
        }


    }

    private PageResult<Area> getAreaPageResult(Integer pageNum, Integer pageSize, List<Area> areas) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Area> pb = new PageInfo<>(areas);
        PageResult<Area> result = new PageResult<>(pb.getPageNum(), pb.getPageSize(), pb.getTotal(),pb.getList());
        return result;
    }

    @Override
    public Result addArea(Area area, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            area.setAddPerson(user.getUsername());
            area.setAddDate(new Date());
            areaMapper.insertSelective(area);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateArea(Area area, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            area.setUpdatePerson(user.getUsername());
            area.setUpdateDate(new Date());
            areaMapper.updateByPrimaryKeySelective(area);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public void deleteArea(Integer aid) {
        areaMapper.deleteByPrimaryKey(aid);
    }
}